//
//  CommonUtils.m
//  帮惠Mall
//
//  Created by reew on 2017/9/15.
//  Copyright © 2017年 reew. All rights reserved.
//

#import "CommonUtils.h"
#import <SVProgressHUD.h>
@implementation CommonUtils
+(BOOL)isPhoneNum:(NSString *)num{
    NSString *predicate = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", predicate];
    
    return [regextestmobile evaluateWithObject:num];
}
+(AFHTTPSessionManager *)getAFHTTPSessionManager{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/javascript", @"text/html",@"image/png",nil];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    manager.responseSerializer =[AFJSONResponseSerializer serializer];//以json格式返回
    manager.requestSerializer = [AFJSONRequestSerializer serializer];//以json格式提交
    manager.requestSerializer.timeoutInterval = 10.0f;
    return manager;
}
+(void)sendPOST:(NSString *)URLString parameters:(id)parameters constructingBodyWithBlock:(void (^)(id<AFMultipartFormData>))block progress:(void (^)(NSProgress * _Nonnull))uploadProgress success:(void (^)(NSURLSessionDataTask *, id))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/javascript", @"text/html",@"image/png",nil];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    manager.responseSerializer =[AFJSONResponseSerializer serializer];//以json格式返回
    manager.requestSerializer = [AFJSONRequestSerializer serializer];//以json格式提交
    manager.requestSerializer.timeoutInterval = 10.0f;
    [manager POST:URLString parameters:parameters constructingBodyWithBlock:block progress:uploadProgress success:success failure:failure];
    
}
+ (void)showAlertViewWithTitle:(NSString *)title Message:(NSString *)message vc:(UIViewController *)vc
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    }];
    
    [alertController addAction:otherAction];
    
    [vc presentViewController:alertController animated:YES completion:nil];
    
}
+(void)saveLoginfoWith:(BOOL)isMember{
    if (isMember) {
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isMember"];
    }else{
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isMember"];
    }
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
    
    [[NSUserDefaults standardUserDefaults] synchronize];
}
+(UIViewController *)getVCWithSBName:(NSString *)name VCIdetifiy:(NSString *)identifier{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:name bundle:[NSBundle mainBundle]];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:identifier];
    return vc;
}
+(void)saveSymbolNoWith:(NSString *)info{
    [[NSUserDefaults standardUserDefaults] setObject:info forKey:@"symbol"];
     [[NSUserDefaults standardUserDefaults] synchronize];
}
+(UINavigationController *)creatNaVcWithStoryboardName:(NSString *)name NaVcIdentifier:(NSString *)identifier{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:name bundle:[NSBundle mainBundle]];
    UINavigationController *navc = [sb instantiateViewControllerWithIdentifier:identifier];
    return navc;
}
+(UITabBarController *)getHomeVC{
    UITabBarController *vc = [[UITabBarController alloc] init];
    //子视图
    UINavigationController *meNav = [CommonUtils creatNaVcWithStoryboardName:@"Main" NaVcIdentifier:@"me"];
    meNav.tabBarItem.image = [UIImage imageNamed:@"My2"];
    meNav.tabBarItem.selectedImage = [UIImage imageNamed:@"My"];
    meNav.tabBarItem.title = @"我的";
    [vc addChildViewController:meNav];
    return vc;
}
+ (UINavigationController *)getLoginVC{
    UIStoryboard *sb2 = [UIStoryboard storyboardWithName:@"LoginStoryboard" bundle:[NSBundle mainBundle]];
    
    UINavigationController *navc = [sb2 instantiateViewControllerWithIdentifier:@"login"];
    return navc;
}

+(void)showHudWithStatus:(HUDStatus)status stayTime:(float)time info:(NSString *)info{
    switch (status) {
        case NomormalStatus:
            [SVProgressHUD setMaximumDismissTimeInterval:time];
            [SVProgressHUD showInfoWithStatus:info];
            break;
        case SuccessStatus:
            [SVProgressHUD setMaximumDismissTimeInterval:time];
            [SVProgressHUD showSuccessWithStatus:info];
            break;
        case ErrorStatus:
            [SVProgressHUD setMaximumDismissTimeInterval:time];
            [SVProgressHUD showErrorWithStatus:info];
            break;
        case LoadingStatus:
            [SVProgressHUD showWithStatus:info];
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];            
            break;
        default:
            break;
    }
}
+(void)dismisHud{
    [SVProgressHUD dismiss];
}

+(CGFloat)getStringWidthWithFontSize:(CGFloat)font string:(NSString *)str andHeight:(CGFloat)height{
    UIFont *temp = [UIFont systemFontOfSize:font];
    NSDictionary *attributes = @{NSFontAttributeName : temp};
    CGRect sizeFit = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    return sizeFit.size.width+16;
}
@end
